package com.woniu.controller;

import com.woniu.common.Assert;
import com.woniu.enumeration.BusinessCode;
import com.woniu.service.CartService;
import com.woniu.util.JwtUtils;
import com.woniu.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
@Slf4j
@Api(tags = "购物车接口")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 查询购物车
     * */
    @GetMapping
    @ApiOperation(value = "查询购物车")
    public ResponseEntity<CartVo> getCart(@RequestHeader(name = "Authorization",required = false)String token) {
        log.info("用户token:{}", token);

        // 查询用户ID
        String userId = jwtUtils.getValue(token, "userId");
        log.info("用户ID:{},",userId);
        // 调用业务层查询购物车
        CartVo cart = cartService.getCart(Long.valueOf(userId));
        log.info("购物车信息:{}", cart);
        return ResponseEntity.ok(cart);
    }

    /**
     * 添加商品到 购物车
     */
    @PostMapping("/{bookId}")
    @ApiOperation("添加商品到购物车")
    public ResponseEntity<CartVo> addCartItem(
            @PathVariable("bookId") Long bookId,
            @RequestHeader(name = "Authorization",required = false)String token) {
        // 获取用户ID
        String userId = jwtUtils.getValue(token, "userId");
        // 调用业务 将 商品添加到 购物车
        CartVo cartVo = cartService.addCartItem(Long.valueOf(userId), bookId);
        return ResponseEntity.ok(cartVo);
    }

    /**
     * 根据 ID删除 购物车图书
     * */
    @DeleteMapping("/{bookId}")
    @ApiOperation(value = "根据ID删除购物车图书")
    public ResponseEntity<CartVo> delete(@PathVariable("bookId") Long bookId,
                                         @RequestHeader(name = "Authorization",required = false)String token) {
        String userId = jwtUtils.getValue(token, "userId");
        // 调用业务删除
        CartVo cartVo = cartService.removeCartItem(Long.valueOf(userId), bookId);
        return ResponseEntity.ok(cartVo);
    }


    /**
     * 清空购物车
     * */
    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "清空购物车")
    public ResponseEntity<CartVo> deleteAll(@RequestHeader(name = "Authorization",required = false)String token) {
        String userId = jwtUtils.getValue(token, "userId");
        // 调用业务删除
        CartVo cartVo = cartService.clearCartItem(Long.valueOf(userId));
        return ResponseEntity.ok(cartVo);
    }

    /**
     * 修改书籍数量
     * */
    @PostMapping("/{bookId}/{quantity}")
    @ApiOperation(value = "修改书籍数量")
    public ResponseEntity<CartVo> updateQuantity(@PathVariable("bookId") Long bookId,
                                                 @PathVariable("quantity") Integer quantity,
                                                 @RequestHeader(name = "Authorization",required = false)String token) {
        String userId = jwtUtils.getValue(token, "userId");
        // 调用业务 修改书籍数量
        CartVo cartVo = cartService.updateCartItem(Long.valueOf(userId), bookId,quantity);
        return ResponseEntity.ok(cartVo);
    }

    /**
     * 在 结算页面显示 购买的商品
     * */
    @GetMapping("/myCartDetails")
    @ApiOperation(value = "结算的商品")
    public ResponseEntity<CartVo> myCartDetails(@RequestHeader(name = "Authorization",required = false)String token) {
        // 获取用户id
        String value = jwtUtils.getValue(token, "userId");

        // 从redis获取购物车数据
        CartVo cartVo = cartService.getCart(Long.valueOf(value));

        return ResponseEntity.ok(cartVo);
    }


}




























